package com.esm.sm800.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Needle;
import com.esm.sm800.database.manager.NeedleCenter;
import com.esm.sm800.util.JtComm;

import java.util.ArrayList;

import zuo.biao.library.base.BaseActivity;

//电机调试
public class DjtsActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "DjtsActivity";

    public static Intent createIntent(Context context) {
        return new Intent(context, DjtsActivity.class);
    }

    private final int[] CTR_IDS = {
            R.id.ctr_1, R.id.ctr_2, R.id.ctr_3, R.id.ctr_4,
            R.id.ctr_5, R.id.ctr_6, R.id.ctr_7, R.id.ctr_8,
            R.id.ctr_9, R.id.ctr_10, R.id.ctr_11, R.id.ctr_12,
    };
    //仓位背景
    private final int[] CBK_IDS = {
            R.id.ctlbk_1, R.id.ctlbk_2, R.id.ctlbk_3, R.id.ctlbk_4,
            R.id.ctlbk_5, R.id.ctlbk_6, R.id.ctlbk_7, R.id.ctlbk_8,
            R.id.ctlbk_9, R.id.ctlbk_10, R.id.ctlbk_11, R.id.ctlbk_12,
    };

    //仓位图片
    private final int[] STOCK_IDS = {
            R.id.stock_1, R.id.stock_2, R.id.stock_3, R.id.stock_4,
            R.id.stock_5, R.id.stock_6, R.id.stock_7, R.id.stock_8,
            R.id.stock_9, R.id.stock_10, R.id.stock_11, R.id.stock_12,
    };

    //仓位-针型号
    private final int[] ZXH_IDS = {
            R.id.tv_xh1, R.id.tv_xh2, R.id.tv_xh3, R.id.tv_xh4,
            R.id.tv_xh5, R.id.tv_xh6, R.id.tv_xh7, R.id.tv_xh8,
            R.id.tv_xh9, R.id.tv_xh10, R.id.tv_xh11, R.id.tv_xh12,
    };

    //仓位-针存量
    private final int[] CL_IDS = {
            R.id.tv_cl1, R.id.tv_cl2, R.id.tv_cl3, R.id.tv_cl4,
            R.id.tv_cl5, R.id.tv_cl6, R.id.tv_cl7, R.id.tv_cl8,
            R.id.tv_cl9, R.id.tv_cl10, R.id.tv_cl11, R.id.tv_cl12,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_djgl);

        findViewById(R.id.tv_back).setOnClickListener(this);

        for (int i = 0; i < CTR_IDS.length; i++) {
            findViewById(CTR_IDS[i]).setOnClickListener(this);
        }

        _needleList = NeedleCenter.getInstance().getNeedlelist();
        for (Needle needle : _needleList) {
            updatestock(needle);
        }
    }

    ArrayList<Needle> _needleList;

    private void updatestock(Needle needle) {
        if (needle == null) {
            return;
        }
        int i = needle.getPosition() - 1;
        if (i < 0 || i >= CBK_IDS.length) {
            return;
        }
        ((TextView) findViewById(ZXH_IDS[i])).setText(getString(R.string.sjm_tx_mc) + needle.getName());
        ((TextView) findViewById(CL_IDS[i])).setText(getString(R.string.sjm_tx_cl) + needle.getStock());
        findViewById(CBK_IDS[i]).setBackgroundColor(Color.parseColor("#FFFFFF"));
        ((TextView) findViewById(ZXH_IDS[i])).setTextColor(Color.parseColor("#000000"));
        ((TextView) findViewById(CL_IDS[i])).setTextColor(Color.parseColor("#454444"));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                onStock(v.getId());
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }

    private void onStock(int id) {
        int position = 0;//选中的仓位
        for (int i = 0; i < CTR_IDS.length; i++) {
            if (CTR_IDS[i] == id) {
                position = i + 1;
                break;
            }
        }

        if (position < 1) {
            return;
        }

        showShortToast(getString(R.string.tiaoshi) + position + getString(R.string.haodianji));
        JtComm.getInstance().sendCommand_chuzhen();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
    }

    void loadStockbmp(Needle needle) {
        if (needle == null) {
            return;
        }
        String imagePath = "" + needle.getImg();
        // 使用BitmapFactory解码图片文件
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        if (bitmap == null) {
            return;
        }

        int i = needle.getPosition() - 1;
        if (i < 0 || i >= CBK_IDS.length) {
            return;
        }
        // 将Bitmap设置到ImageView中
        ImageView imageView = findViewById(STOCK_IDS[i]);
        imageView.setImageBitmap(bitmap);


    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
    // 假设你已经有了图片文件的路径


}

